@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/browser/{applicationId}/{endpointId}"
@page "/browser/{discovererId}/{applicationId}/{supervisorId}/{endpointId}"

@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.OpcUa.Api.Core.Models;
@using Microsoft.Azure.IIoT.OpcUa.Api.Publisher;
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Models;
@using Microsoft.Azure.IIoT.App.Services.SecureData;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;

@inject NavigationManager NavigationManager
@inject Services.Browser BrowseManager
@inject Services.Publisher Publisher
@inject PublisherServiceEvents PublisherServiceEvents
@inject Registry RegistryHelper
@inject UICommon CommonHelper
@inject Blazored.SessionStorage.ISessionStorageService sessionStorage
@inject SecureData secureData
@inject IRegistryServiceApi registryService

@inherits IIoTItemsCollection<ListNode>

<h1>Endpoint Control</h1>

@if (EndpointModel != null)
{
    <div class="subtitle">
        Url: @EndpointModel.Registration.EndpointUrl
        Discoverer: @EndpointModel.Registration.DiscovererId
        Site: @EndpointModel.Registration.SiteId
        Security Mode: @EndpointModel.Registration.Endpoint.SecurityMode
        Security Policy: @CommonHelper.ExtractSecurityPolicy(EndpointModel.Registration.Endpoint.SecurityPolicy)
        Security Level: @EndpointModel.Registration.SecurityLevel
    </div>
}

<div>
    @if (DiscovererId != string.Empty && DiscovererId != RegistryHelper.PathAll)
    {
        <a href="/discoverers">/ @DiscovererId</a>
    }
    @if (ApplicationId != string.Empty && ApplicationId != RegistryHelper.PathAll)
    {
        <a href="/applications">/ @CommonHelper.ApplicationUri[ApplicationId]</a>
    }
    @if (SupervisorId != string.Empty && SupervisorId != RegistryHelper.PathAll)
    {
        <a href="/supervisors">/ @SupervisorId</a>
    }

    @if (BrowseManager.Path != null)
    {
        var index = 0;
        foreach (var item in BrowseManager.Path)
        {
            int i = ++index;
            if (item.Equals(BrowseManager.Path.Last()))
            {
                <span>/ @item.ToString()</span>
            }
            else
            {
                <a href="javascript: void(0)" @onclick="@(() => GetTreeBackAsync(Items.Results[0].NextParentId, Items.Results[0].ParentIdList, i))">
                    / @item.ToString()
                </a>
            }
        }
    }
</div>

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-small">Name</th>
            <th class="width-medium-large">Node Id</th>
            <th class="width-small">Node Type</th>
            <th class="width-medium-large">Value</th>
            <th class="width-small">Publishing</th>
            <th class="width-small">Publishing Config</th>
            <th class="width-small">Status</th>
            <th class="width-xsmall">
                @if (Items.Results.Count > 0)
                {
                    if (Items.Results[0].ParentName != "Root")
                    {
                        <a aria-label="back" href="javascript: void(0)"
                           @onclick="@(() => GetTreeBackAsync(Items.Results[0].NextParentId, Items.Results[0].ParentIdList, Items.Results[0].ParentIdList.Count - 1))">
                            <span class="oi oi-arrow-left"></span>
                        </a>
                    }
                    else
                    {
                        if (Items.Results[0].DiscovererId != null)
                        {
                            var link = @NavigationManager.BaseUri + "endpoints/" + Items.Results[0].DiscovererId + "/" + @ApplicationId + "/" + @SupervisorId;
                            <a aria-label="back" href="@link">
                                <span class="oi oi-arrow-left"></span>
                            </a>
                        }
                    }
                }
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var node in Items.Results)
        {
            <tr>
                <td class="hover-text width-medium">
                    <div>
                        @node.NodeName
                    </div>
                </td>
                <td class="hover-text width-medium-large">
                    <div>
                        @node.Id
                    </div>
                </td>
                @if (node.Children && (node.NodeClass != NodeClass.Method))
                {
                    <td class="hover-text width-small">
                        <div>
                            <a href="javascript: void(0)" @onclick="@(() => GetTreeAsync(node.Id, node.ParentIdList))">
                                @node.NodeClass
                            </a>
                        </div>
                    </td>
                }
                else if (node.NodeClass == NodeClass.Method)
                {
                    <td class="hover-text width-small">
                        <div>
                            <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(node, Drawer.Action))">
                                @node.NodeClass
                            </a>
                        </div>
                    </td>
                }
                else
                {
                    <td class="hover-text width-small">
                        <div>
                            @node.NodeClass
                        </div>
                    </td>
                }
                @if (node.NodeClass == NodeClass.Variable && node.AccessLevel.HasFlag(NodeAccessLevel.CurrentRead) ||
               node.AccessLevel.HasFlag(NodeAccessLevel.CurrentWrite))
                {
                    <td class="width-medium-large">
                        <div class="value-scroll">
                            <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(node, Drawer.Action))">
                                <span>@node.Value</span>
                                <span>(@node.DataType)</span>
                                <span>(@node.Status -</span>
                                <span> @node.Timestamp)</span>
                            </a>
                        </div>
                    </td>
                }
                else
                {
                    <td class="hover-text width-small">
                        <div>
                            N/A
                        </div>
                    </td>
                }

                @if (node.NodeClass == NodeClass.Variable)
                {
                    <td class="width-small">
                        @{string pubStatus = (node.Publishing == true) ? "Turn off" : "Turn on";}
                        <button class="btn btn-primary btn-table-cell" @onclick="@(() => SetPublishingAsync(EndpointId, node))">@pubStatus</button>
                    </td>
                    <td class="width-small space-wrap">
                        <label>Publishing: </label><a href="javascript: void(0)" @onclick="@(() => OpenDrawer(node, Drawer.Publisher))">
                            @(!IsTimeIntervalSet(node.PublishedItem?.PublishingInterval) ? "1000" : node.PublishedItem.PublishingInterval?.TotalMilliseconds.ToString()) ms<br />
                        </a>
                        <label>Sampling:</label><a href="javascript: void(0)" @onclick="@(() => OpenDrawer(node, Drawer.Publisher))">
                            @(!IsTimeIntervalSet(node.PublishedItem?.SamplingInterval) ? "1000" : node.PublishedItem.SamplingInterval?.TotalMilliseconds.ToString()) ms<br />
                        </a>
                        <label>HeartBeat: </label><a href="jpavascript: void(0)" @onclick="@(() => OpenDrawer(node, Drawer.Publisher))">
                            @(!IsTimeIntervalSet(node.PublishedItem?.HeartbeatInterval) ? "-" : node.PublishedItem.HeartbeatInterval?.TotalSeconds.ToString() + " sec")
                            <br />
                        </a>
                    </td>
                }
                else
                {
                    <td class="hover-text width-small">
                        <div>
                            N/A
                        </div>
                    </td>
                    <td class="hover-text width-small">
                        <div>
                            N/A
                        </div>
                    </td>
                }
                @if (node.ErrorMessage != null)
                {
                    <td class="hover-text width-small">
                        <div>
                            @node.ErrorMessage
                        </div>
                    </td>
                }
                else
                {
                    <td class="hover-text width-small">
                        <div>
                            OK
                        </div>
                    </td>
                }
            </tr>
        }
    </tbody>
</table>
<div class="@_tableEmpty center">No Nodes found.</div>

<ErrorMessage PageError="@Items.Error" Status="@null"></ErrorMessage>

<Drawer HeaderText="Select Action" ObjectData="@NodeData" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    @if (DrawerType == Drawer.Action)
    {
        <_DrawerActionContent NodeData="@NodeData" EndpointId="@EndpointId" PagedNodeList="@Items" Credential="@Credential"></_DrawerActionContent>
    }
    else
    {
        <_DrawerPublisherContent NodeData="@NodeData" Onclick="((NodeData) => ClickHandlerAsync((ListNode)NodeData))"></_DrawerPublisherContent>
    }
</Drawer>

<LoadMore Result=@Items IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />

<Spinner IsLoading=@IsLoading></Spinner>